package sunyu.controller.hbase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sunyu.service.hbase.HbaseService;
import sunyu.tools.hbase.pojo.Row;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/api/hbase")
public class ApiHbaseController {

    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    @Resource(name = "hbaseService")
    private HbaseService hbaseService;

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed, @PathVariable("did") String did, @PathVariable("pageSize") Integer pageSize) {
        return hbaseService.find("tab", did, null, null, reversed, null, pageSize, true, null, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed, @PathVariable("did") String did, @PathVariable("pageSize") Integer pageSize, @PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime) {
        return hbaseService.find("tab", did, startTime, endTime, reversed, null, pageSize, true, null, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startRowName}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed, @PathVariable("did") String did, @PathVariable("pageSize") Integer pageSize, @PathVariable("startRowName") String startRowName) {
        return hbaseService.find("tab", did, null, null, false, null, pageSize, false, startRowName, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startRowName}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed, @PathVariable("did") String did, @PathVariable("pageSize") Integer pageSize, @PathVariable("startRowName") String startRowName, @PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime) {
        return hbaseService.find("tab", did, startTime, endTime, false, null, pageSize, false, startRowName, null, null);
    }

    @RequestMapping(value = "/countCan/{did}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Long countCan(@PathVariable("did") String did) {
        return hbaseService.count("tab", did, null, null, null);
    }

    @RequestMapping(value = "/countCan/{did}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Long countCan(@PathVariable("did") String did, @PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime) {
        return hbaseService.count("tab", did, startTime, endTime, null);
    }

}
